from django.contrib.auth.backends import ModelBackend
from shopping_mall.utils.secret import  SecretOauth
from django.db.models import Q
from apps.users.models import User
from django.conf import settings

class LoginBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = User.objects.get(Q(username=username)|Q(mobile=username))

        except Exception as e:
            return None

        if user.check_password(password):
            return user

# 返回邮箱的加密链接
def generate_verify_email_url(request):

    data_dict = {
        "user_id": request.user.id,
        "email": request.user.email
    }
    dumps_dict = SecretOauth().dumps(data_dict)

    verify_url = settings.EMAIL_VERIFY_URL + dumps_dict

    return verify_url